package com.erpoa.controller;

import com.alibaba.fastjson.JSON;
import com.erpoa.annotation.NoRepeatSubmit;
import com.erpoa.entity.MgUser;
import com.erpoa.service.PdaLoginService;
import com.erpoa.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @author chenjimin
 * @Title: PdaLoginController
 * @ProjectName oaerp
 * @Description: 登陆新接口
 * @date 2020/9/111:16
 */
@CrossOrigin(maxAge = 3600)
@Api(value = "PdaLoginController", tags = {"分厂PDA新登录接口"})
@RestController
@RequestMapping("/PDAService")
public class PdaLoginController {
    private static final Logger logger = LoggerFactory.getLogger(PdaLoginController.class);
    @Autowired
    private PdaLoginService pdaLoginService;
    @Value("${userpda.serversion}")
    private Integer serversion;

    @ApiOperation("PDA新登录接口")
    @RequestMapping(value = {"/login"}, method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    @NoRepeatSubmit(key = "pdaNewLogin:arg[0]")
    public String pdaLoginServlet(@RequestBody MgUser user) throws IOException {
        MgUser mgUser = this.pdaLoginService.getUserFromDataBase(user.getsCode());
        if (StringUtil.isNull(mgUser)) {
            return getJsonOfUser(new MgUser());
        }
        String status = this.pdaLoginService.getPDAStatus(user.getsName());
        String version = "0";
        try {
            this.pdaLoginService.updateorInsertPdaVersion(user.getsName(), user.getVersion(), user.getsCode());
            if (StringUtil.isNotEmpty(user.getVersion())) {
                version = user.getVersion();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        logger.debug("已获取用户：" + getJsonOfUser(mgUser) + "状态：" + status);
        System.out.println("已获取用户：" + getJsonOfUser(mgUser) + "状态：" + status);
        if ("OK".equals(status)) {
            boolean check = this.checkLogin(mgUser, user.getsPassword());
            if (check) {
                mgUser.setVersion(version);
               /* if(StringUtil.isNotEmpty(user.getVersion())){
                    if (Integer.valueOf(user.getVersion())<=serversion){
                    mgUser.setsName("你的设备版本号过低，请更新软件版本后再使用！");
                    return getJsonOfUser(mgUser);}
                }*/
                logger.debug("登陆成功：" + getJsonOfUser(mgUser));
                System.out.println("登陆成功：" + getJsonOfUser(mgUser));
                return getJsonOfUser(mgUser);
            } else {
                logger.debug("登陆失败：" + mgUser.getsName() + "，密码错误!");
                System.out.println("登陆失败：" + mgUser.getsName() + "，密码错误!");
                return getJsonOfUser(new MgUser());
            }
        } else {
            logger.debug("当前设备号：" + user.getsName() + "，PDA尚未注册,请先注册!");
            System.out.println("当前设备号：" + user.getsName() + "，PDA尚未注册,请先注册!");
            mgUser.setsName(status);
            return getJsonOfUser(mgUser);
        }
    }
    private String getJsonOfUser(MgUser user) {
        String json = JSON.toJSONString(user);
        try {
            return json;
        } catch (Exception e) {
            e.printStackTrace();
            return "";
        }
    }

    /**
     * 密码验证比对
     *
     * @param user
     * @param password
     * @return
     */
    private boolean checkLogin(MgUser user, String password) {
        String pass = user.getsPassword();
        if (pass == null || "".equals(pass)) {
            return false;
        } else {
            if (password.equals(pass.toLowerCase())) {
                return true;
            } else {
                return false;
            }
        }
    }

}
